package practice;

import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;

public class Day24 {
    public static volatile AtomicInteger count = new AtomicInteger(0);
//    public volatile int count = 0;
    private final Object locker = new Object();
    public void add() {
        count.incrementAndGet();
    }
    private ExecutorService executorService = Executors.newFixedThreadPool(100);
    public void countDown() throws InterruptedException {
        CountDownLatch c = new CountDownLatch(10);
        List<Agent> list = new LinkedList<>();
        for(int i = 0; i < 100; i++) {
            list.add(new Agent());
        }
        for(Agent agent : list) {
            agent.countDownLatch = c;
            executorService.submit(agent);
        }
        c.await();
        executorService.shutdown();
    }

    public int findLongestChain(int[][] pairs) {
        Arrays.sort(pairs,(a,b) -> {
            return a[0] - b[0];
        });
        int n = pairs.length;
        int[] dp = new int[n];
        int max = 1;
        for(int i = 0; i < n; i++) {
            dp[i] = 1;
            for(int j = 0; j < i; j++) {
                if(pairs[i][0] > pairs[j][0] && pairs[i][0] > pairs[j][1]) {
                    dp[i] = dp[j] + 1;
                }
            }
            max = Math.max(max,dp[i]);
        }
        return max;
    }

    public int numberOfArithmeticSlices(int[] nums) {
        int n = nums.length;
        int[][] dp = new int[n][n];
        Map<Long,List<Integer>> map = new HashMap<>();
        for(int i = 0; i < n; i++) {
            if(map.getOrDefault((long)nums[i],null) == null) {
                map.put((long)nums[i],new ArrayList<>());
            }
            map.get((long)nums[i]).add(i);
        }
        int ret = 0;
        for(int j = 2; j < n; j++) {
            for(int i = 1; i < j; i++) {
                long c = 2L * nums[i] - nums[j];
                if(map.containsKey(c)) {
                    List<Integer> list = map.get(c);
                    for(int k = 0; k < list.size(); k++) {
                        int index = list.get(k);
                        if(index < i) {
                            dp[i][j] += (1+dp[index][i]);
                        }
                    }
                }
                ret += dp[i][j];
            }
        }
        return ret;
    }

    public static void main(String[] args) throws InterruptedException {
        Day24 d = new Day24();
        d.countDown();
        System.out.println(Day24.count);
//        ExecutorService executorService = Executors.newFixedThreadPool(100);
//           for(int i = 0; i < 1000; i++) {
//                executorService.submit(()->{
//                    try {
//                        d.add();
//                        Thread.sleep(500);
//                    } catch (InterruptedException e) {
//                        throw new RuntimeException(e);
//                    }
//                });
//           }
//           Thread.sleep(500);
//        System.out.println(d.count);
//        executorService.shutdown();
    }
}
